/*
 * 
 */
package ged.diagram.edit.parts;

import ged.Link;
import ged.customizationCode.functions.CustomizationCodeLinkEditPart;
import ged.customizationCode.shapes.connections.PipeConnection;
import ged.diagram.edit.policies.LinkItemSemanticEditPolicy;
import ged.extension.point.definition.handlers.EvaluateContributionConnectionShapes;
import ged.customizationCode.shapes.source_target_Decorations.CircleDecoration;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.ConnectionEndpointLocator;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.PolylineDecoration;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionNodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ITreeBranchEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.draw2d.ui.figures.PolylineConnectionEx;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;

/**
 * @generated
 */
public class LinkEditPart extends ConnectionNodeEditPart implements
		ITreeBranchEditPart {

	/**
	 * @generated
	 */
	public static final int VISUAL_ID = 4001;

	PolylineConnection polyLine;

	//create a new instance of the class with the methods used for customisation of the links
	CustomizationCodeLinkEditPart customizationCodeLinkEditPart = new CustomizationCodeLinkEditPart();

	/**
	 * @generated
	 */
	public LinkEditPart(View view) {
		super(view);
	}

	/**
	 * @generated
	 */
	protected void createDefaultEditPolicies() {
		super.createDefaultEditPolicies();
		installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE,
				new LinkItemSemanticEditPolicy());
	}

	/**
	 * Creates figure for this edit part.
	 * 
	 * Body of this method does not depend on settings in generation model
	 * so you may safely remove <i>generated</i> tag and modify it.
	 * 
	 * @generated NOT
	 */
	protected Connection createConnectionFigure() {
		Link link = (Link) resolveSemanticElement();
		//shape of the connections
		ged.ConnectionShapes selected_shape = link.getShape_of_connection();
		String customConnectionShape = link.getLineShape();
		if (customConnectionShape == null)
			customConnectionShape = "";
		if (!customConnectionShape.equals("")) {
			Map mapConnectionShapes = new HashMap<String, PolylineConnection>();
			EvaluateContributionConnectionShapes instanceImage = EvaluateContributionConnectionShapes
					.getInstance();
			mapConnectionShapes = instanceImage.evaluateConnectionShapes();
			//Move next key and value of Map by iterator
			Iterator<Entry<String, PolylineConnection>> Connection_Shapes_Iterator = mapConnectionShapes
					.entrySet().iterator();
			String connection_shape_key = "";
			PolylineConnection connection_shape_value = null;
			while (Connection_Shapes_Iterator.hasNext()) {
				Map.Entry Connection_Shapes_Pairs = (Map.Entry) Connection_Shapes_Iterator
						.next();
				connection_shape_key = (String) Connection_Shapes_Pairs
						.getKey();
				if (connection_shape_key.equals(customConnectionShape)) {
					connection_shape_value = (PolylineConnection) Connection_Shapes_Pairs
							.getValue();
					polyLine = connection_shape_value;
				}
			}
		}
		if (selected_shape != null && polyLine == null) {
			switch (selected_shape) {
			case PIPE_CONNECTION:
				polyLine = new PipeConnection();
				break;
			default:
				polyLine = new PolylineConnectionEx();
				break;
			}
		}

		// set target label
		String sourceLabel = link.getSourceLabel();
		if (sourceLabel == null) {
			sourceLabel = "";
		}
		if (!sourceLabel.equals("")) {
			WrappingLabel connectionSourceLabel = new WrappingLabel();
			connectionSourceLabel.setText(link.getSourceLabel());
			ConnectionEndpointLocator sourceRoleLocator = new ConnectionEndpointLocator(
					polyLine, false);
			sourceRoleLocator.setVDistance(15);
			polyLine.add(connectionSourceLabel, sourceRoleLocator);
			/*polyLine.getLayoutManager().setConstraint(connectionSourceLabel,
					new MidpointLocator(polyLine, 0));*/
		}

		// set source label
		String targetLabel = link.getTargetLabel();
		if (targetLabel == null) {
			targetLabel = "";
		}
		if (!targetLabel.equals("")) {
			WrappingLabel connectionSourceLabel = new WrappingLabel();
			connectionSourceLabel.setText(link.getTargetLabel());
			ConnectionEndpointLocator targetRoleLocator = new ConnectionEndpointLocator(
					polyLine, true);
			targetRoleLocator.setVDistance(15);
			polyLine.add(connectionSourceLabel, targetRoleLocator);
		}

		//change the width of the line
		ged.LineWidth line_width = link.getConn_linewidth();
		if (line_width != null)
			polyLine.setLineWidth(line_width.getLwidth());

		//change the type of the line
		int type = customizationCodeLinkEditPart.changeLineType(link);
		polyLine.setLineStyle(type);

		//set the background colour of the line ????????????
		/*ged.Color line_backgr_color = link.getConnection_backgroundColor();
		if (line_backgr_color != null)
			polyLine.setBackgroundColor(new Color(Display.getCurrent(),
					line_backgr_color.getRed(), line_backgr_color.getGreen(),
					line_backgr_color.getBlue()));*/

		//set the foreground colour of the line ?????????????
		/*		ged.Color line_foregr_color = link.getConnection_foregroundColor();
		 if (line_foregr_color != null) {
		 polyLine.setForegroundColor(new Color(Display.getCurrent(),
		 line_foregr_color.getRed(), line_foregr_color.getGreen(),
		 line_foregr_color.getBlue()));
		 }*/

		//set the colour of the line
		//((org.eclipse.gmf.runtime.notation.Edge) getModel()) = getPrimaryView()
		/*		ged.Color line_foregr_color = link.getConnection_foregroundColor();
		 if (line_foregr_color != null)
		 {
		 int red = line_foregr_color.getRed();
		 int green = line_foregr_color.getGreen();
		 int blue = line_foregr_color.getBlue();
		 LineStyle style = (LineStyle)((org.eclipse.gmf.runtime.notation.Edge) getModel()).getStyle(NotationPackage.eINSTANCE.getLineStyle()); 
		 style.setLineColor(red+green+blue);
		 }*/

		//line target decoration
		ged.LineEndDecoration targer_dec = link.getLine_target_decoration();
		if (targer_dec != null) {
			switch (targer_dec) {
			case POLY_LINE:
				polyLine.setTargetDecoration(new PolylineDecoration());
				break;
			case POLYGON:
				polyLine.setTargetDecoration(new PolygonDecoration());
				break;
			case CIRCLE:
				polyLine.setTargetDecoration(new CircleDecoration());
				break;
			default:
				polyLine.setTargetDecoration(null);
				break;
			}
		}

		//line source decoration
		ged.LineEndDecoration source_dec = link.getLine_source_decoration();
		if (source_dec != null) {
			switch (source_dec) {
			case POLY_LINE:
				polyLine.setSourceDecoration(new PolylineDecoration());
				break;
			case POLYGON:
				polyLine.setSourceDecoration(new PolygonDecoration());
				break;
			case CIRCLE:
				polyLine.setSourceDecoration(new CircleDecoration());
				break;
			default:
				polyLine.setSourceDecoration(null);
				break;
			}
		}

		return (Connection) polyLine;
	}

	/**
	 * @generated
	 */
	public PolylineConnectionEx getPrimaryShape() {
		return (PolylineConnectionEx) getFigure();
	}
}
